<?php

namespace app\admin\controller;
class Tender extends base
{
    //前置方法
    protected $beforeActionList = [
        'map'  =>  ['only'=>'index'],
    ];

    /**
     * 搜索
     */
    protected function map(){
        if ($this->request->isGet()) {
            $data = $this->request->param();
            $this->datas = array();
            if (!empty($data['start_time']) && !empty($data['end_time']) && (strtotime($data['start_time']) < strtotime($data['end_time']))) {
                $this->datas ['create_time'] = [
                    ['>', strtotime($data['start_time'])],
                    ['<', strtotime($data['end_time'])]
                ];
            }
            if (isset($data['status'])) {
                $this->datas ['status'] = $data['status'];
            }
            if (!empty($data['cid'])) {
                $this->datas ['cid'] = $data['cid'];
            }
            if (!empty($data['title'])) {
                $this->datas ['title'] = ['like', '%' . $data['title'] . '%'];
            }
            $this->datas['lang'] = cookie('think_var_admin');
        }
    }


    /**
     * 保存
     */
    public function save()
    {
        if (!$this->request->isPost()) {
            return ajax_info('请求失败');
        }
        $data = $this->request->post();

        if(!empty($data['id'])){
            return $this->update($data);
        }
        $data['uid'] = $this->getLoginUser()->id;
        $data['uname'] = $this->getLoginUser()->name;
        $data['lang']=cookie('think_var_admin');

        $res = $this->model->allowField(true)->save($data);
        if ($res) {
            return ajax_info('保存成功','y');
        } else {
            return ajax_info('添加失败');
        }
    }

    /**
     * 查看
     * @param $id
     */
    public function read($id){


        if (intval($id) < 1) {
            $this->error('数据错误');
        }

        $data = $this->model->find($id);
        /*列表start*/

        $list =  db('tenderapply')->alias('T')
            ->join('__USER__ U', 'U.id= T.uid')
            ->field('U.name,U.QQ,U.mail,U.mobile,T.center,T.money,T.create_time')
            ->where('T.tid', $id)
            ->paginate()
            ->appends($this->request->param());

        $this->assign([
            'list' => $list,
            'data' => $data,
        ]);

        return $this->fetch();

    }
}
